<template>
  <div class="user-card">
    <h2>用户管理示例</h2>
    <p class="description">演示 Vuex 的用户状态管理和持久化</p>
    
    <div v-if="!isAuthenticated" class="login-section">
      <h3>登录</h3>
      <div class="login-form">
        <input 
          v-model="loginForm.username" 
          type="text" 
          placeholder="用户名"
          class="input"
        />
        <input 
          v-model="loginForm.password" 
          type="password" 
          placeholder="密码"
          class="input"
        />
        <button @click="handleLogin" class="btn btn-primary" :disabled="loggingIn">
          {{ loggingIn ? '登录中...' : '登录' }}
        </button>
      </div>
    </div>
    
    <div v-else class="user-info-section">
      <div class="user-profile">
        <div class="avatar">
          <img v-if="user.avatar" :src="user.avatar" alt="头像" />
          <div v-else class="avatar-placeholder">{{ username.charAt(0).toUpperCase() }}</div>
        </div>
        <div class="user-details">
          <h3>{{ username }}</h3>
          <p class="email">{{ email }}</p>
          <p class="summary">{{ userSummary }}</p>
        </div>
      </div>
      
      <div class="user-actions">
        <button @click="handleLogout" class="btn btn-danger">登出</button>
        <button @click="showEditForm = !showEditForm" class="btn btn-secondary">
          {{ showEditForm ? '取消编辑' : '编辑资料' }}
        </button>
      </div>
      
      <div v-if="showEditForm" class="edit-form">
        <h4>编辑资料</h4>
        <input 
          v-model="editForm.name" 
          type="text" 
          placeholder="新用户名"
          class="input"
        />
        <input 
          v-model="editForm.email" 
          type="email" 
          placeholder="新邮箱"
          class="input"
        />
        <button @click="handleUpdateProfile" class="btn btn-primary">保存</button>
      </div>
    </div>
    
    <div class="state-info">
      <h4>当前状态</h4>
      <pre>{{ stateInfo }}</pre>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from 'vuex'

export default {
  name: 'User',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      editForm: {
        name: '',
        email: ''
      },
      loggingIn: false,
      showEditForm: false
    }
  },
  computed: {
    ...mapState('user', ['user', 'isLoggedIn', 'token']),
    ...mapGetters('user', ['username', 'email', 'isAuthenticated', 'userSummary']),
    
    stateInfo() {
      return {
        user: this.user,
        isLoggedIn: this.isLoggedIn,
        token: this.token ? '已设置' : '未设置'
      }
    }
  },
  mounted() {
    // 页面加载时恢复登录状态
    this.$store.dispatch('user/restoreLogin')
  },
  methods: {
    ...mapActions('user', ['login', 'logout', 'updateProfile']),
    
    async handleLogin() {
      if (!this.loginForm.username) {
        alert('请输入用户名')
        return
      }
      
      this.loggingIn = true
      try {
        await this.login({
          username: this.loginForm.username,
          password: this.loginForm.password
        })
        this.loginForm = { username: '', password: '' }
        alert('登录成功！')
      } catch (error) {
        alert('登录失败：' + error.message)
      } finally {
        this.loggingIn = false
      }
    },
    
    handleLogout() {
      this.logout()
      this.showEditForm = false
      alert('已登出')
    },
    
    handleUpdateProfile() {
      const updates = {}
      if (this.editForm.name) updates.name = this.editForm.name
      if (this.editForm.email) updates.email = this.editForm.email
      
      if (Object.keys(updates).length === 0) {
        alert('请输入要更新的信息')
        return
      }
      
      this.updateProfile(updates)
      this.editForm = { name: '', email: '' }
      this.showEditForm = false
      alert('资料已更新！')
    }
  }
}
</script>

<style scoped>
.user-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

h2 {
  margin: 0 0 8px 0;
  color: #2c3e50;
}

.description {
  color: #7f8c8d;
  margin: 0 0 24px 0;
  font-size: 14px;
}

.login-section,
.user-info-section {
  margin: 24px 0;
}

.login-section h3,
.user-info-section h3 {
  margin: 0 0 16px 0;
  color: #2c3e50;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 300px;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 16px;
  margin: 24px 0;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  overflow: hidden;
  background: #ecf0f1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: bold;
  color: #3498db;
}

.user-details h3 {
  margin: 0 0 4px 0;
  color: #2c3e50;
}

.email {
  margin: 4px 0;
  color: #7f8c8d;
  font-size: 14px;
}

.summary {
  margin: 4px 0;
  color: #95a5a6;
  font-size: 12px;
}

.user-actions {
  display: flex;
  gap: 12px;
  margin: 16px 0;
}

.edit-form {
  margin: 24px 0;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.edit-form h4 {
  margin: 0 0 12px 0;
  color: #2c3e50;
}

.edit-form .input {
  margin-bottom: 12px;
}

.state-info {
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #ecf0f1;
}

.state-info h4 {
  margin: 0 0 12px 0;
  color: #2c3e50;
  font-size: 14px;
}

.state-info pre {
  background: #2c3e50;
  color: #ecf0f1;
  padding: 12px;
  border-radius: 6px;
  font-size: 12px;
  overflow-x: auto;
}

.input {
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  width: 100%;
  box-sizing: border-box;
}

.input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #3498db;
  color: white;
}

.btn-secondary {
  background: #95a5a6;
  color: white;
}

.btn-danger {
  background: #e74c3c;
  color: white;
}
</style>

